//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;

namespace OpenLS.UI.Interop
{
    internal abstract class WindowMessageProcessor
    {
        protected readonly RibbonWindow _owner;

        protected WindowMessageProcessor(RibbonWindow owner)
        {
            _owner = owner; 
        }

        internal abstract IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled);

        internal static IntPtr OnGetMinMaxInfo(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
#if SPREADSHEET_DEBUG
            Debug.WriteLine("OnGetMinMaxInfo");
#endif
            NativeMethods.DefWindowProc(hwnd, msg, wParam, lParam);
            NativeMethods.MinMaxInfo mmi = NativeMethods.MinMaxInfo.GetFrom(lParam);
            bool isCompositionEnabled = NativeMethods.IsCompositionEnabled();
            IntPtr monitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST);
            IntPtr topLeftMonitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTOPRIMARY);
            if (monitor != IntPtr.Zero)
            {
                var monitorInfo = new NativeMethods.MonitorInfo();
                monitorInfo.Initialize();
                double border = !isCompositionEnabled ? SystemParameters.BorderWidth : 0;
                var border1 = (int) border;
                var border2 = (int) (border*2);
                NativeMethods.GetMonitorInfo(monitor, ref monitorInfo);
                var topLeftMonitorInfo = new NativeMethods.MonitorInfo();
                NativeMethods.GetMonitorInfo(topLeftMonitor, ref topLeftMonitorInfo);
                RECT rcWorkArea = monitorInfo.rcWork;
                RECT myArea = topLeftMonitorInfo.rcMonitor;
                if (!isCompositionEnabled)
                    myArea = topLeftMonitorInfo.rcWork;
                mmi.ptMaxPosition.X = Math.Abs(myArea.Left) - border1;
                mmi.ptMaxPosition.Y = Math.Abs(myArea.Top) - border1;
                mmi.ptMaxSize.X = Math.Abs(rcWorkArea.Right - rcWorkArea.Left + border2); 
                if (false)
                 mmi.ptMaxSize.Y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top ); //\\
                else
                    mmi.ptMaxSize.Y = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top + border2); //\\
#if SPREADSHEET_DEBUG
                Debug.WriteLine("mmi.ptMaxPosition" + mmi.ptMaxPosition   + " mmi.PtMaxSize " + mmi.ptMaxSize  + " mmi.pTMaxTrackSize " + mmi.ptMaxTrackSize +
                    " mmi.pt_minTrackSize " + mmi.ptMinTrackSize);
#endif
                Marshal.StructureToPtr(mmi, lParam, true);

            }
            handled = true;
            return IntPtr.Zero;
        }
    }
}